// 组合总和3

package Leetcode;

import java.util.ArrayList;
import java.util.List;

class Solution_216_1 {
    public List<List<Integer>> result = new ArrayList<>();

    public List<List<Integer>> combinationSum3(int k, int n) {
        backTrack(k, n, 1, 0, new ArrayList<>());
        return result;
    }

    public void backTrack(int k, int n, int num, int sum, List<Integer> combination) {
        if (k == 0 && sum == n)
            result.add(new ArrayList<>(combination));

        if (sum > n)
            return;

        for (int i = num; i <= 9; i++) {
            combination.add(i);
            backTrack(k - 1, n, i + 1, sum + i, combination);
            combination.remove(combination.size() - 1);
        }
    }
}
